using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_CTDonHangUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDropDownList();
        }
    }
    private void LoadDropDownList()
    {
        tbDonHangCollection dhCollection = tbDonHangBUS.GetAll();
        ddl_donhang.DataTextField = tbDonHangColumns.MaDH.ToString();
        ddl_donhang.DataValueField = tbDonHangColumns.MaDH.ToString();
        ddl_donhang.DataSource = dhCollection;
        ddl_donhang.DataBind();
        ddl_donhang.Items.Insert(0, new ListItem("All", "0"));

        tbSanPhamCollection spCollection = tbSanPhamBUS.GetAll();
        ddl_sanpham.DataTextField = tbSanPhamColumns.TenSP.ToString();
        ddl_sanpham.DataValueField = tbSanPhamColumns.MaSP.ToString();
        ddl_sanpham.DataSource = spCollection;
        ddl_sanpham.DataBind();
        ddl_sanpham.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        try
        {
            int iD=int.Parse (Request .QueryString ["iD"].ToString ());
            txt_mactdonhang .Text =iD .ToString ();
            tbCTDonHang ctdonhang=tbCTDonHangBUS .GetByID (iD);
            ddl_donhang .SelectedValue =ctdonhang .MaDH .ToString ();
            ddl_sanpham .SelectedValue =ctdonhang .SanPham .ToString ();
            txt_soluong .Text =ctdonhang .SoLuong .ToString ();
            txt_dongia .Text =ctdonhang .DonGia .ToString ();
        }
        catch 
        {
            Response .Redirect ("~/Admin/CTDonHangDanhSach.aspx");
        }
    }

    protected void btn_sua_Click(object sender, EventArgs e)
    {
        try
        {
            int iD=int.Parse (Request .QueryString ["iD"].ToString ());
            tbCTDonHang ctdonhang=tbCTDonHangBUS .GetByID (iD);
            ctdonhang .MaDH =int.Parse (ddl_donhang .SelectedValue );
            ctdonhang .SanPham =int.Parse (ddl_sanpham .SelectedValue );
            ctdonhang .SoLuong =int.Parse (txt_soluong .Text );
            ctdonhang .DonGia =int.Parse (txt_dongia .Text );
            tbCTDonHangBUS .Update (ctdonhang );

        }
        catch 
        {
        }
        Response .Redirect ("~/Admin/CTDonHangDanhSach.aspx");
        Response .Redirect (Request .RawUrl );
    }
}
